package com.lizhenyao.waimai.controller;

import com.lizhenyao.waimai.config.ProjectUrlConfig;
import com.lizhenyao.waimai.constant.CookieConstant;
import com.lizhenyao.waimai.constant.RedisConstant;
import com.lizhenyao.waimai.dataobject.SellerInfo;
import com.lizhenyao.waimai.enums.ResultEnum;
import com.lizhenyao.waimai.service.ISellerService;
import com.lizhenyao.waimai.utils.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author: lizhenyao
 * @Desciption:
 */
@Controller
@RequestMapping("/seller")
public class SellerUserController {

    @Autowired
    private ISellerService iSellerService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ProjectUrlConfig projectUrlConfig;

    @GetMapping("/login")
    public ModelAndView login(@RequestParam("openid") String openid,
                              HttpServletResponse response,
                              Map<String, Object> map){
        //1.将openid与数据库中做匹配,不匹配就直接返回,没权限
        //todo 由于没有开放平台账号,所以扫码登录的功能实现不了
//        SellerInfo sellerInfo = iSellerService.findSellerInfoByOpenid(openid);
//        if (sellerInfo == null){
//            map.put("msg", ResultEnum.LOGIN_FAIL.getMsg());
//            map.put("url", "/sell/seller/order/list");
//            return new ModelAndView("common/error", map);
//        }
        //2.设置token至redis
        String token = UUID.randomUUID().toString();
        Integer expire = RedisConstant.EXPIRE;
        redisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_PREFIX, token), openid, expire, TimeUnit.SECONDS);

        //3.设置token值cookie
        CookieUtil.set(response, CookieConstant.TOKEN, token, expire);

        return new ModelAndView("redirect:" + projectUrlConfig.getSell() + "/sell/seller/order/list");

    }

    @GetMapping("/logout")
    public ModelAndView logout(HttpServletResponse response,
                               HttpServletRequest request,
                               Map<String, Object> map){
        //1.查询cookie
        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
        if (cookie != null){
            //2.删除redis中openid信息
            redisTemplate.opsForValue().getOperations().delete(String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue()));
            //3.删除cookie中的token信息
            CookieUtil.set(response, CookieConstant.TOKEN, null, 0);
        }

        map.put("msg", ResultEnum.LOGOUT_SUCCESS.getMsg());
        map.put("url", "/sell/seller/order/list");
        return new ModelAndView("common/success", map);
    }
}
